package com.ash.controller.book;

import com.ash.domain.Books;
import com.ash.domain.User;
import com.ash.domain.UserInfo;
import com.ash.domain.vo.BooksVo;
import com.ash.service.book.BookService;
import com.ash.service.book.BookTypeService;
import com.ash.service.comment.CommentService;
import com.ash.service.type.TypeService;
import com.ash.util.JsonUtil;
import com.ash.util.MyFileUtil;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/3/22 19:41
 * @file : project06.com.ash.controller.book.bookController.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Controller
public class BookController {
    @Resource
    BookService bookService;
    @Resource
    TypeService typeService;
    @Resource
    CommentService commentService;
    @Resource
    BookTypeService bookTypeService;
    /**
     * 页面跳转方法
     * @param mode mode
     * @return book
     */
    @RequestMapping("/book/toList")
    public String toList(Model mode){
        System.out.println("==========toList页面=========");
        mode.addAttribute("type",JsonUtil.getJsonByObject(typeService.selectAll(null,null)));
        mode.addAttribute("list",JsonUtil.getJsonByObject(bookService.selectAll()));
        return "user/bookList";
    }

    /**
     *
     * @param page 第几页
     * @param limit 每页容量
     * @return 返回要审核的书籍Json
     */
    @ResponseBody
    @RequestMapping("/book/selectToCheck")
    public String selectToCheck(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        return JsonUtil.getJsonList(bookService.selectToCheckCount(),bookService.selectToCheck(page,limit));
    }

    /**
     * 根据id列表进行批量审核
     * @param bids 要审核的id列表
     * @return 审核结果
     */
    @PostMapping("/book/checkByIds")
    @ResponseBody
    public Integer checkByIds(@Nullable @RequestParam("ids")List<Integer> bids){
        return bookService.checkByIds(bids);
    }
    @RequestMapping("/book/bookCheckById")
    @ResponseBody
    public Integer bookCheckById(@RequestParam("bid")Integer bid){
        System.out.println("bid:"+bid);
        return bookService.check(bid);
    }
    /**
     * 查询书籍列表
     * @param bookTypes 书籍类别列表
     * @param searchName 要模糊搜索的书籍名称
     * @return 查询的书籍列表
     */
    @ResponseBody
    @PostMapping(value = "/book/list")
    public List<Books> list(@Nullable @RequestParam("bookTypes") List<Integer> bookTypes, @Nullable @RequestParam("searchName") String searchName){
        System.out.println("==============ajax数据异步获取================");
        System.out.println(bookTypes);
        System.out.println(searchName);
        return bookService.selectByTypeAndName(bookTypes,searchName);
    }
    @RequestMapping("/book/bookDetail")
    public String bookDetail(Integer bid, Model model, HttpServletRequest request){
        model.addAttribute("book",bookService.selectById(bid));
        request.setAttribute("commentCount",commentService.selectDataCount(bid));
        return "user/bookDetail";
    }
    @RequestMapping("/book/selectCountBookByBid")
    @ResponseBody
    public Integer selectCountByBid(@RequestParam("bid") Integer bid ){
        return bookService.selectById(bid).getCount();
    }

    @RequestMapping("/book/bookEdit")
    public String bookEdit(){
        return "admin/bookEdit";
    }
    @RequestMapping("/book/bookCheck")
    public String bookCheck(){
        return "admin/bookCheck";
    }
    @RequestMapping("/book/selectAll")
    @ResponseBody
    public String selectAll(@Nullable @RequestParam("bName") String bName, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        /*查询数据*/
        List<BooksVo> booksVoList = bookService.selectAll(bName, page, limit);
        for (BooksVo booksVo : booksVoList) {
            System.out.println(booksVo);
        }
        return JsonUtil.getJsonList(bookService.dataCount(bName), booksVoList);
    }
    @RequestMapping("/book/deleteByIds")
    @ResponseBody
    public Integer deleteByIds(@RequestParam("ids")List<Integer> ids){
        return bookService.deleteByIds(ids);
    }
    @RequestMapping("/book/delete")
    @ResponseBody
    public Integer deleteByIds(@RequestParam("bid")Integer bid){
        return bookService.delete(bid);
    }
    @RequestMapping("/user/book/add")
    public String add(Model model){
        model.addAttribute("type",JsonUtil.getJsonByObject(typeService.selectAll(null,null)));
        return "user/addBook";
    }
    @PostMapping("/user/book/add")
    @ResponseBody
    public Integer addBooks(Books books,@RequestParam("ids")List<Integer> ids,HttpSession session){
        books.setUrl((String) session.getAttribute("bookUrl"));
        books.setIsChecked((byte)0);
        bookService.add(books);
        bookTypeService.addType(books.getBid(),ids);
        return 1;
    }
    @RequestMapping("/user/book/bookUrl")
    @ResponseBody
    public Integer bookUrl(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request, HttpSession session){
        User user =(User)request.getSession().getAttribute("user");
        /*上传文件*/
        File upload = MyFileUtil.upload(request.getSession().getServletContext().getRealPath("/img/book/user/" + user.getUname()), file);
        String absolutePath = upload.getAbsolutePath();
        session.setAttribute("bookUrl",absolutePath.substring(absolutePath.indexOf("img") - 1));
        return 1;
    }
    /**
     * 查询书籍列表
     * @return 查询的书籍列表
     */
    @ResponseBody
    @RequestMapping(value = "/book/likeList")
    public List<BooksVo> likeList(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        Integer mid = userInfo.getMid();
        Integer grade = userInfo.getGrade();
        if(mid==null||grade==null){
            return null;
        }
        return bookService.selectLikeAll(mid,grade,null,null);
    }
}
